#include "cmsis_os2.h"
#include "stm32f103xe.h"
#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_def.h"
#include "stm32f1xx_hal_gpio.h"

// const osThreadAttr_t defaultTask_attributes = {
//     .name = "defaultTask",
//     .stack_size = 128 * 4,
//     .priority = (osPriority_t) osPriorityNormal,
//   };

// typedef struct {
//     const char                   *name;   ///< name of the thread
//     uint32_t                 attr_bits;   ///< attribute bits
//     void                      *cb_mem;    ///< memory for control block
//     uint32_t                   cb_size;   ///< size of provided memory for control block
//     void                   *stack_mem;    ///< memory for stack
//     uint32_t                stack_size;   ///< size of stack
//     osPriority_t              priority;   ///< initial thread priority (default: osPriorityNormal)
//     TZ_ModuleId_t            tz_module;   ///< TrustZone module identifier
//     uint32_t                  reserved;   ///< reserved (must be 0)
//   } osThreadAttr_t;

const osThreadAttr_t attr_led0 = {
    .name = "led0 task",
    .stack_size = 128 * 4,
    .priority = osPriorityHigh,
};

const osThreadAttr_t attr_led1 = {
    .name = "led0 task",
    .stack_size = 128 * 4,
    .priority = osPriorityLow,
};

// osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) 

static void led0_task(void *arg)
{
    static uint8_t st = 0;
    while (1) {
        st = ~st;
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, st);
        osDelay(50);
    }
}

static void led1_task(void *arg)
{
    static uint8_t st = 0;
    while (1) {
        st = ~st;
        HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, st);
        osDelay(20);
    }
}

void app_thread_create(void)
{
    osThreadNew(led0_task, NULL, &attr_led0);
    osThreadNew(led1_task, NULL, &attr_led1);
}
